//
//  CorporateView.m
//  HomePage
//
//  Created by xiong qi on 13-5-5.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "CorporateView.h"
#import "../../ResourceLoader/ResourceLoader/ResourceLoader.h"
#import "../../ResourceLoader/ResourceLoader/TemplateFactory.h"

#define AnimationDuration 0.4

@implementation CorporateView
@synthesize currentMiddleView,rootPath;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.pagingEnabled = NO;
        self.clipsToBounds = NO;
        self.decelerationRate = 0.1;
        self.delegate = self;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.clipsToBounds = YES;
        isCanMove = YES;
        
        [self setBackgroundColor:[UIColor clearColor]];
        
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        self.rootPath= [NSString stringWithFormat:@"%@/paddata/company/Corporate",[paths objectAtIndex:0]];
        
        contentArray = [[[ResourceLoader getInstance]getTemplatesByPath:rootPath] retain];
        
        [self renderTemplate];
    }
    return self;
}

-(void)renderTemplate
{
    if(contentArray&&[contentArray count]>0)
    {
        int largeviewwidth  = TemplateWidthMiddle;
        int largeviewheight = TemplateHeightMiddle;

        self.contentSize = CGSizeMake((([contentArray count]-1)/6+1)*self.frame.size.width,self.frame.size.height);
        
        for(NSInteger i=0;i<[contentArray count];i++)
        {
            TemplateView * tView = [[TemplateFactory getInstance] createViewByTemplateFileName:[contentArray objectAtIndex:i] RootPath:rootPath];
            [self addSubview:tView];
            [tView release];
            tView.delegate = self;
            tView.tag = i+100;
            [tView renderTemplate];
            
            if (i == 0) {
                tView.frame = CGRectMake((self.frame.size.width-largeviewwidth)/2, (self.frame.size.height-largeviewheight)/2, largeviewwidth, largeviewheight);
                [tView TransmutationMiddle];
                tView.center = CGPointMake(self.center.x, self.bounds.size.height/2);
                self.currentMiddleView = tView;
            }
            else
            {
                CGFloat offsetX = i*(TemplateWidth+TemplateSpaceX)+(largeviewwidth-TemplateWidth)/2;
                tView.center = CGPointMake(self.center.x + offsetX, self.bounds.size.height/2);
            }
        }
    }
    self.contentSize = CGSizeMake((([contentArray count]-1)*(TemplateWidth+TemplateSpaceX)+self.frame.size.width+TemplateWidth/2), self.frame.size.height);
}

-(void)ViewBeTouch:(TemplateView *)view
{
    
}
//-(void)TouchMiddle:(TemplateView *)view
//{
//    if (view == currentMiddleView) {
//        [self InitMediaView];
//    }
//    else
//    {
//        [self MoveToView:view];
//    }
//    
//}
//
//
#pragma mark ----UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (isCanMove) {
        [self ChangeFrame:scrollView.contentOffset.x];
    }
    
}

//#pragma mark----Touch
//-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    if (currentType == 1) {
//        currentType = 0;
//        [UIView animateWithDuration:AnimationDuration
//                         animations:^{
//                             
//                             currentMiddleView.bounds = CGRectMake(0, 0, TemplateWidth, TemplateHeight);
//                             [currentMiddleView TransmutationSmall];
//                             
//                             TemplateView * tempview = nil;
//                             for(NSInteger i=0;i<[contentArray count];i++)
//                             {
//                                 tempview = (TemplateView *)[self viewWithTag:100+i];
//                                 tempview.alpha = 0;
//                             }
//                         }
//                         completion:^(BOOL finished){
//                             isCanMove = NO;
//                             
//                             self.pagingEnabled = YES;
//                             self.clipsToBounds = YES;
//                             self.frame = originRect;
//                             self.contentSize = CGSizeMake((([contentArray count]-1)/6+1)*self.frame.size.width,self.frame.size.height);
//                             self.contentOffset = CGPointMake(((currentMiddleView.tag-100)/6)*originRect.size.width, 0);
//                             
//                             TemplateView * tempview = nil;
//                             float centerX = 0;
//                             float centerY = 0;
//                             for(NSInteger i=0;i<[contentArray count];i++)
//                             {
//                                 tempview = (TemplateView *)[self viewWithTag:100+i];
//                                 
//                                 //一面放6个，放不下的放到另一面
//                                 int page = i/6;
//                                 int row = (i%6)/3;
//                                 int col = (i%6)%3;
//                                 
//                                 //间隙横向20 纵向20
//                                 centerX = page * self.frame.size.width + TemplateSpaceX + col*(TemplateWidth+TemplateSpaceX)+TemplateWidth/2;
//                                 centerY = row * (TemplateHeight+TemplateSpaceY)+TemplateTopSpace+TemplateHeight/2;
//                                 
//                                 tempview.center = CGPointMake(centerX, centerY);
//                             }
//                             
//                             currentMiddleView = nil;
//                             
//                             [UIView animateWithDuration:AnimationDuration
//                                              animations:^{
//                                                  
//                                                  TemplateView * tempview = nil;
//                                                  for(NSInteger i=0;i<[contentArray count];i++)
//                                                  {
//                                                      tempview = (TemplateView *)[self viewWithTag:100+i];
//                                                      tempview.alpha = 1.0;
//                                                      
//                                                  }
//                                                  
//                                              }
//                                              completion:^(BOOL finished){
//                                                  
//                                                  
//                                              }];
//                         }];
//        
//    }
//}
//
#pragma  mark ----Other
-(void)MoveToView:(TemplateView *)view
{
    if (view.tag == 100) {
        [self setContentOffset:CGPointZero animated:YES];
    }
    else if(view.tag == [contentArray count]+99)
    {
        [self setContentOffset:CGPointMake(self.contentSize.width-self.frame.size.width, 0) animated:YES];
    }
    else
    {
        [self setContentOffset:CGPointMake(self.center.x-(TemplateHeightMiddle-TemplateWidth)/2 + (view.tag-101)*(TemplateWidth+TemplateSpaceX)+TemplateWidthMiddle/2+TemplateSpaceX+TemplateWidth/2-self.superview.center.x, 0) animated:YES];
    }
}


-(void)ChangeFrame:(float)offsetx
{
    int spaceX = offsetx+self.frame.size.width/2;
    int centerY = self.bounds.size.height/2;
    //左滑
    if (spaceX-TemplateWidthMiddle/4>currentMiddleView.center.x) {
        
        if(currentMiddleView.tag<[contentArray count]+99)
        {
            TemplateView * nextview = (TemplateView *)[self viewWithTag:currentMiddleView.tag+1];
            
            [UIView animateWithDuration:AnimationDuration
                             animations:^{
                                 nextview.bounds = CGRectMake(0, 0, TemplateWidthMiddle, TemplateHeightMiddle);
                                 currentMiddleView.bounds = CGRectMake(0, 0, TemplateWidth, TemplateHeight);
                                 
                                 //当前视图是第一个视图
                                 if (currentMiddleView.tag == 100) {
                                     currentMiddleView.center = CGPointMake(self.center.x-(TemplateHeightMiddle-TemplateWidth)/2, centerY);
                                     nextview.center = CGPointMake(currentMiddleView.center.x + ((TemplateWidth+TemplateWidthMiddle)/2+TemplateSpaceX), centerY);
                                     
                                     TemplateView * tempview = nil;
                                     for (int i=2; i<[contentArray count]; i++) {
                                         
                                         tempview = (TemplateView *)[self viewWithTag:100+i];
                                         UIView * preview = [self viewWithTag:i+99];
                                         
                                         if (preview == nextview) {
                                             tempview.center = CGPointMake(nextview.center.x + ((TemplateWidth+TemplateWidthMiddle)/2+TemplateSpaceX),centerY);
                                         }
                                         else
                                         {
                                             tempview.center = CGPointMake(preview.center.x+(TemplateWidth+TemplateSpaceX),centerY);
                                         }
                                     }
                                 }
                                 else
                                 {
                                     UIView * preview = [self viewWithTag:currentMiddleView.tag-1];
                                     currentMiddleView.center = CGPointMake(preview.center.x+(TemplateWidth+TemplateSpaceX), centerY);
                                     nextview.center = CGPointMake(currentMiddleView.center.x + ((TemplateWidth+TemplateWidthMiddle)/2+TemplateSpaceX), centerY);
                                 }
                                 
                                 [currentMiddleView TransmutationSmall];
                                 [nextview TransmutationMiddle];
                                 currentMiddleView = nextview;
                             }
                             completion:^(BOOL finished){
                                 
                             }];
        }
    }
    //右滑
    else if(currentMiddleView.center.x>spaceX+TemplateWidthMiddle/4)
    {
        if (currentMiddleView.tag != 100) {
            
            [UIView animateWithDuration:AnimationDuration
                             animations:^{
                                 
                                 TemplateView * preview = (TemplateView *)[self viewWithTag:currentMiddleView.tag-1];
                                 preview.bounds = CGRectMake(0, 0, TemplateWidthMiddle, TemplateHeightMiddle);
                                 currentMiddleView.bounds = CGRectMake(0, 0, TemplateWidth, TemplateHeight);
                                 
                                 //如果当前是最后一个视图
                                 if (currentMiddleView.tag == [contentArray count]+99) {
                                     currentMiddleView.center = CGPointMake(self.contentSize.width-(self.frame.size.width/2 - (TemplateHeightMiddle-TemplateWidth)/2), centerY);
                                     
                                     preview.center = CGPointMake(currentMiddleView.center.x - ((TemplateWidth+TemplateWidthMiddle)/2+TemplateSpaceX), centerY);
                                     
                                     TemplateView * tempview = nil;
                                     for (int i=[contentArray count]-3; i>=0; i--) {
                                         
                                         tempview = (TemplateView *)[self viewWithTag:100+i];
                                         UIView * nextview = [self viewWithTag:101+i];
                                         
                                         if (preview == nextview) {
                                             tempview.center = CGPointMake(preview.center.x - ((TemplateWidth+TemplateWidthMiddle)/2+TemplateSpaceX), centerY);
                                         }
                                         else
                                         {
                                             tempview.center = CGPointMake(nextview.center.x-(TemplateWidth+TemplateSpaceX), centerY);
                                         }
                                     }
                                     
                                     
                                 }
                                 else
                                 {
                                     UIView * nextview = [self viewWithTag:currentMiddleView.tag+1];
                                     currentMiddleView.center = CGPointMake(nextview.center.x-(TemplateWidth+TemplateSpaceX),centerY);
                                     preview.center = CGPointMake(currentMiddleView.center.x - ((TemplateWidth+TemplateWidthMiddle)/2+TemplateSpaceX), centerY);
                                     
                                 }
                                 
                                 [currentMiddleView TransmutationSmall];
                                 [preview TransmutationMiddle];
                                 currentMiddleView = preview;
                             }
                             completion:^(BOOL finished){
                             }];
        }
        
        
    }
}

//-(void)InitMediaView
//{
//    if (mediaview == nil) {
//        mediaview = [[MediaView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
//    }
//    mediaview.alpha = 0;
//    
//    if(currentMiddleView.playPath == nil)
//    {
//        return;
//    }
//    
//    if (currentMiddleView.type == ImageTemplateType) {
//        [mediaview Refresh:currentMiddleView.playPath Type:0];
//    }
//    else if(currentMiddleView.type == VidelTemplateType)
//    {
//        //        [mediaview Refresh:currentMiddleView.playPath Type:1];
//        return;
//    }
//    else if(currentMiddleView.type == MixTemplateType)
//    {
//        [mediaview Refresh:currentMiddleView.playPath Type:0];
//    }
//    
//    
//    
//    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
//    [window.rootViewController.view addSubview:mediaview];
//    
//    [self ShowMediaView];
//}
//
//-(void)ShowMediaView
//{
//    [UIView animateWithDuration:AnimationDuration
//                     animations:^{
//                         
//                         mediaview.alpha = 1.0;
//                     }
//                     completion:^(BOOL finished){
//                         
//                         
//                     }];
//}


-(void)dealloc
{
    self.currentMiddleView = nil;
    self.rootPath = nil;
    [super dealloc];
}
@end
